package day8.homeworkII;

import com.sun.corba.se.impl.encoding.CDROutputObject;

import java.io.*;

public class Task2 {

    public static void main(String[] args) throws IOException {
        String file = "C:\\Git";
        String file2 = "D:\\d1";
        System.out.println(new File(file2).getAbsolutePath());

        recursiveFiles(new File(file), new File(file2));

    }

    private static void recursiveFiles(File file, File file2) throws IOException {
        //获取当前文件夹下所有文件
        File[] files = file.listFiles();

        //循环
        for (File f : files) {
            //判断当前的文件路径是文件还是文件夹
            if(f.isDirectory()) {
                new File(file2.getAbsolutePath() + "\\" + f.getName()).mkdirs();
                recursiveFiles(f, new File(file2.getAbsoluteFile()+ "\\" + f.getName()));
            }else {
                copy(f, new File(file2.getAbsolutePath() + "\\" + f.getName()));
            }
        }
    }

    public static void copy(File file, File file2) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(file2);

        try {
            byte[] bbuf = new byte[102400];
            int hasRead = 0;
            while ((hasRead = fis.read(bbuf)) > 0) {
                fos.write(bbuf, 0, hasRead);
            }
        } catch (IOException E){
            E.printStackTrace();
        } finally {
            fos.flush();
            fis.close();
            fos.close();
        }
    }
}
